import 'lead_model.dart';

class ThingSmartALinkModeModel {
  final String? title;

  ///< Title of linkMode
  final String? desText;

  ///< Descriptions
  /// EZ=1 AP=2 ZIGSUB=3 QRCode=4 NB=5 GPRS=6 BT=7 WC=8 433SUB=9 CaptureDevice2=10 QRCode2=11 CaptureDevice=1000
  final int linkMode;

  ///< Current pairing type
  final List<ThingSmartALeadModel>? leadList;

  ///< Multi-step assisted guidance
  final bool updatedTag;

  ///< Whether modified on IoT platform
  final ThingSmartALeadModel? singleLead; // Single-step assisted guidance

  ThingSmartALinkModeModel({
    this.title,
    this.desText,
    this.linkMode = 0,
    this.leadList,
    this.updatedTag = false,
    this.singleLead,
  });

  factory ThingSmartALinkModeModel.fromJson(Map<String, dynamic> json) {
  // 处理 singleLead 字段的类型转换
  final ThingSmartALeadModel? singleLead;
  if (json['singleLead'] != null) {
    final singleLeadJson = json['singleLead'] is Map<String, dynamic>
        ? json['singleLead'] as Map<String, dynamic>
        : Map<String, dynamic>.from(json['singleLead'] as Map<dynamic, dynamic>);
    singleLead = ThingSmartALeadModel.fromJson(singleLeadJson);
  } else {
    singleLead = null;
  }

  // 处理 leadList 字段的类型转换
  final List<ThingSmartALeadModel>? leadList;
  if (json['leadList'] != null) {
    leadList = (json['leadList'] as List).map((e) {
      final leadJson = e is Map<String, dynamic>
          ? e as Map<String, dynamic>
          : Map<String, dynamic>.from(e as Map<dynamic, dynamic>);
      return ThingSmartALeadModel.fromJson(leadJson);
    }).toList();
  } else {
    leadList = null;
  }

  return ThingSmartALinkModeModel(
    title: json['title'] as String?,
    desText: json['desText'] as String?,
    linkMode: (json['linkMode'] as int?) ?? 0,
    leadList: leadList,
    updatedTag: (json['updatedTag'] as bool?) ?? false,
    singleLead: singleLead,
  );
}

  Map<String, dynamic> toJson() {
    return {
      'title': title,
      'desText': desText,
      'linkMode': linkMode,
      'leadList': leadList?.map((e) => e.toJson()).toList(),
      'updatedTag': updatedTag,
      'singleLead': singleLead?.toJson(),
    };
  }
}
